package mylab.io.store.mqtt.property;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.net.InetSocketAddress;
import java.net.URI;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;
import mylab.io.base.model.Terminal;
import mylab.io.base.property.BaseDasProperties;
import mylab.io.base.property.DasProperties;
import mylab.io.base.util.TerminalUtil;
import mylab.utils.common.NetUtil;

@Getter
@Setter
public class MqttClientProperties extends BaseDasProperties {
    static final String PROPERTIES_PREFIX = DasProperties.PROPERTIES_PREFIX + ".mqtt.client";

    /**
     * uri of mqtt server
     * eg: {@code tcp://127.0.0.1:1883}
     * eg: {@code ssl://127.0.0.1:1884}
     */
    String broker;
    String clientId;
    String username;
    String password;
    boolean reconnect;

    @JsonIgnore
    public Terminal getBrokerTerminal() {
        URI uri = getBrokerUri();
        return TerminalUtil.socketTerminal(uri.getHost(), uri.getPort());
    }

    @JsonIgnore
    public InetSocketAddress getBrokerAddress() {
        URI uri = getBrokerUri();
        return InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
    }

    @JsonIgnore
    public boolean isSsl() {
        URI uri = getBrokerUri();
        return StrUtil.equalsAnyIgnoreCase(uri.getScheme(), "ssl");
    }

    @JsonIgnore
    public URI getBrokerUri() {
        return NetUtil.getUri(broker);
    }
}
